package com.example.indenture.race.controller;


import com.example.indenture.common.service.ImageService;
import com.example.indenture.common.vo.Result;
import com.example.indenture.common.vo.Search;
import com.example.indenture.race.entity.Race;
import com.example.indenture.race.service.RaceService;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author CY
 * @date 2023/02/21 15:18
 **/
@RestController
@RequestMapping("/api")


public class RaceController {
    @Resource
    private RaceService raceService;

    @Autowired
    private ImageService imageService;


    @RequestMapping("/race/insert")
    public Result insertRace(@RequestBody Race race) {

        if (raceService.existsAnotherRace(race.getName()) != 0) {
            return Result.failed("Duplicate name!");
        }
        race.setUrl(race.getUrl());
        return raceService.insert(race) == 1 ? Result.ok(race) : Result.failed("Add failure!");
    }

    @RequestMapping(value = "/race/delete/{id}")
    public Result<Object> deleteUserById(@PathVariable Integer id) {
        return raceService.delete(id) == 1 ? Result.ok("Deleted successfully!") : Result.failed("Deletion failure!");
    }

    @RequestMapping("/race/update")
    public Result<Object> updateRace(@RequestBody Race race) {
        if (race == null) {
            return Result.failed("update failed");
        }
        return raceService.update(race) == 1 ? Result.ok("Update successfully") : Result.failed("Update failure");
    }

    @RequestMapping("/race/getById/{id}")
    public Result<Object> getRaceById(@PathVariable Integer id) {
        if (id == null) {
            return Result.failed("Get failed by yourself");
        }
        Race temp = raceService.getRaceById(id);
        return temp != null ? Result.ok(temp) : Result.failed("Get failed by severice");
    }

    @RequestMapping("/race/getAll")
    public PageInfo<Race> getAllRaces(@RequestBody Search search) {
        return raceService.getAllRaces(search);
    }

    @RequestMapping("/race/getByUser")
    public PageInfo<Race> getRaceByUser(@RequestBody Receive receive) {
        return raceService.getRacesByUser(receive.getUid(), receive.getSearch());
    }

    @Data
    static class Receive {
        Search search;
        Integer uid;
    }

    @PostMapping(value = "/image/{imageTypeName}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> uploadImage(
            @RequestParam MultipartFile mf,
            @PathVariable String imageTypeName) {
        return imageService.uploadImage(mf, imageTypeName);
    }

    @RequestMapping("/race/get-races-company/{cid}")
    public List<Race> getRacesByCompany(@PathVariable Integer cid) {
        return raceService.getListsByCompany(cid);
    }


}
